
// The Ambiguity of Fishing for Fun, October 2022
// This file cleans and formats the raw survey data for estimation of the logit models

use rawdata.dta, clear
expand 8 
sort id_code region version
by id_code region version: gen set=_n
order id_code region version set
sort region version set 

* Merging with MasterKey.data (describing survey versions)
merge region version set using MasterKey.dta
tab _merge
drop _merge
gen tripchoice=L1_BEST
gen ratea=L1_A
gen rateb=L1_B
gen ratec=L1_C

local i=2 
while `i' <=8 {
replace tripchoice=L`i'_BEST if set==`i'
replace ratea=L`i'_A if set==`i'
replace rateb=L`i'_B if set==`i'
replace ratec=L`i'_C if set==`i'
 local i= `i' + 1
 }

* Formatting for logit model estimation
expand 3 
sort id_code region version set
by id_code region version set: gen trip=_n
order id_code region version set trip
gen chosen=0
by id_code region version set: replace chosen=1 if trip==tripchoice

gen rate=ratea
replace rate=rateb if trip==2
replace rate=ratec if trip==3

gen Priceline=Priceline_a
replace Priceline=Priceline_b if trip==2
replace Priceline=Priceline_c if trip==3

foreach y in a {
foreach x in SFbaglim SFminsize SFcaughtmin SFcaughtmax Scaughtmin BSBcaughtmin WFcaughtmin RDcaughtmin SFkeptmin SFkeptmax Skeptmin BSBkeptmin WFkeptmin RDkeptmin {
gen `x'=`x'`y'
}
}

foreach y in b {
foreach x in SFbaglim SFminsize SFcaughtmin SFcaughtmax Scaughtmin BSBcaughtmin WFcaughtmin RDcaughtmin SFkeptmin SFkeptmax Skeptmin BSBkeptmin WFkeptmin RDkeptmin {
replace `x'=`x'`y' if trip==2
}
}

foreach x in SFbaglim SFminsize SFcaughtmin SFcaughtmax Scaughtmin BSBcaughtmin WFcaughtmin RDcaughtmin SFkeptmin SFkeptmax Skeptmin BSBkeptmin WFkeptmin RDkeptmin {
replace `x'=0 if trip==3
}

rename (Scaughtmin BSBcaughtmin WFcaughtmin RDcaughtmin Skeptmin BSBkeptmin WFkeptmin RDkeptmin) (Scaught BSBcaught WFcaught RDcaught Skept BSBkept WFkept RDkept) 
replace Scaught=0 if (trip<3 & (region==3 | region==4)) 
replace Skept=0 if (trip<3 & (region==3 | region==4))
replace WFcaught=0 if (trip<3 & region==1)
replace WFkept=0 if (trip<3 & region==1)
replace RDcaught=0 if (trip<3 & region!=4)
replace RDkept=0 if (trip<3 & region!=4)

drop if tripchoice==-1 
replace rate=0 if mi(rate)

* Creating variables
bysort id_code region version set: egen rate_max=max(rate)
bysort id_code region version set: egen chosen_max=max(chosen)
bysort id_code region version set: replace chosen=1 if (tripchoice==4 & rate==rate_max)
bysort id_code region version set: replace tripchoice=_n if (tripchoice==4 & rate==rate_max)
bysort id_code region version set: replace chosen=1 if (mi(chosen) & rate==rate_max) 
bysort id_code region version set: replace chosen=1 if (chosen_max==0 & rate==rate_max) 
bysort id_code region version set: replace tripchoice=_n if (mi(tripchoice) & rate==rate_max)
drop if (rate_max==0 & chosen_max==0)

bysort id_code region version set: egen maxim=sum(chosen) 
drop if maxim>1
bysort id_code region version set: gen rate_aux=_n
bysort id_code region version set: egen sumchosen=total(chosen)
bysort id_code region version set: egen max_aux=max(rate_aux)
bysort id_code region version set: replace chosen=0 if (sumchosen>1 & rate_aux!=max_aux)

* Rescaling
gen aux=set 
tostring aux, replace
gen ID=id_code+aux
gen CatchOther=(Scaught+BSBcaught+WFcaught+RDcaught)/100 
gen KeepOther=(Skept+BSBkept+WFkept+RDkept)/100
gen ReleaseOther=CatchOther-KeepOther
order ReleaseOther KeepOther
gen Age=(2010-Year)/100 
replace Priceline=Priceline/1000 
gen opt_out=cond(trip==3,1,0)
egen SFminCaught=min(SFcaughtmin)
egen SFminKept=min(SFkeptmin)
egen SFmaxCaught=max(SFcaughtmax)
egen SFmaxKept=max(SFkeptmax)

* Probabilistic catch outcomes
local i=SFminCaught  
while `i' <=SFmaxCaught {
  gen SFcatch_`i'=`i' 
  local i= `i' + 1
 }
 
  local i=SFminCaught 
while `i' <=SFmaxCaught {
  replace SFcatch_`i'=0 if (SFcatch_`i'<SFcaughtmin | SFcatch_`i'>SFcaughtmax)
  replace SFcatch_`i'=SFcaughtmin if SFcaughtmin==SFcaughtmax
  local i= `i' + 1
 }
 
 local i=SFminCaught
while `i' <=SFmaxCaught {
  gen SFkeep_`i'=SFcatch_`i'
  replace SFkeep_`i'=min(SFkeep_`i',SFkeptmax)
  local i= `i' + 1
 } 

 local i=SFminCaught
while `i' <=SFmaxCaught {
  gen SFrelease_`i'=SFcatch_`i'-SFkeep_`i'
  local i= `i' + 1
 } 
 
  local i=SFminCaught
while `i' <=SFmaxCaught {
  replace SFkeep_`i'=(SFkeep_`i')/100
  replace SFrelease_`i'=(SFrelease_`i')/100
  local i= `i' + 1
 } 
 
 local i=SFminCaught
while `i' <=SFmaxCaught {
  gen Pcatch_`i'=1/(SFcaughtmax-SFcaughtmin+1) if (SFcatch_`i'>=SFcaughtmin & `i'<=SFcaughtmax)
  local i= `i' + 1
 }
 
  local i=SFminCaught 
while `i' <=SFmaxCaught {
  replace Pcatch_`i'=1/(SFmaxCaught-SFminCaught+1) if SFcaughtmin==SFcaughtmax
  local i= `i' + 1
 }
 
  local i=SFminCaught
while `i' <=SFmaxCaught {
  replace Pcatch_`i'=0 if mi(Pcatch_`i')
  local i= `i' + 1
 }

   local i=SFminCaught
while `i' <=SFmaxCaught {
  replace SFcatch_`i'=(SFcatch_`i')/100
  local i= `i' + 1
 }  
 
destring id_code ID, replace
format id_code ID %20.0f
drop if mi(Age) | mi(Male) | mi(Income) | mi(Education) 
drop if Male==-1 | Income==-1 | Education==-1
gen Income_medium=cond(Income>3 & Income<6,1,0)
gen Income_high=cond(Income>5,1,0)
gen Education_college=cond(Education>3 & Education<7,1,0)
gen Education_graduate=cond(Education>6,1,0)
egen Identifier=group(id_code region version set)
replace ID=Identifier
replace Male=0 if Male==2
drop if (Q1_Spring==999 | Q1_Summer==999 | Q1_Fall==999 | Q1_Winter==999)
replace Q1_Spring=0 if mi(Q1_Spring)
replace Q1_Summer=0 if mi(Q1_Summer)
replace Q1_Fall=0 if mi(Q1_Fall)
replace Q1_Winter=0 if mi(Q1_Winter)
gen avidity=(Q1_Spring+Q1_Summer+Q1_Fall+Q1_Winter)
egen max_avidity=max(avidity)
replace avidity=avidity/max_avidity
egen newID=group(ID)
replace ID=newID

keep id_code-trip Male Education Income chosen avidity Priceline SFcaughtmin SFcaughtmax SFkeptmin SFkeptmax ID-opt_out CatchOther ReleaseOther KeepOther SFcatch_0-Identifier
order Age Income_medium Income_high Education_college Education_graduate avidity Male
outsheet using ReadyDataFluke.txt, replace 
save ReadyDataFluke.dta, replace
